﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CustomUIControls;

namespace VoliaStat
{
    public partial class MyTaskbarNotifier : TaskbarNotifier
    {

        MyDoublePie _chart;

        ContentArea _ca_total,_ca_today,_ca_inet,_ca_tv,_ca_overpay,_ca_error;

        List<ContentArea> _areas=new List<ContentArea>();

        public MyTaskbarNotifier():base()
        {
            InitializeComponent();

			SetBackgroundBitmap(new Bitmap(GetType(),"skin.bmp"),Color.FromArgb(255,0,255));
			SetCloseBitmap(new Bitmap(GetType(),"close.bmp"),Color.FromArgb(255,0,255),new Point(300,55));
			TitleRectangle=new Rectangle(144,56,155,14);
			ContentRectangle=new Rectangle(144,74,168,57);

            CloseClickable=true;
			TitleClickable=true;
			ContentClickable=false;
			EnableSelectionRectangle=false;
			KeepVisibleOnMousOver=true;
			ReShowOnMouseOver=true;

            _chart = new MyDoublePie();

            this.Controls.Add(_chart);

            _chart.Location = new Point(7,11);
            _chart.Size = new Size(130,130);

            titleText =  DateTime.Now.ToLongTimeString();

            _ca_total = new ContentArea();
            
            _ca_total.align = StringAlignment.Far;
            _ca_total.brush = Brushes.Red;
            _ca_total.font = new Font("Arial",18,FontStyle.Bold,GraphicsUnit.Pixel);//;normalContentFont;
            _ca_total.rectangle = new Rectangle(144,74,168,20);

            _ca_today= new ContentArea();
            _ca_today.align = StringAlignment.Far;
            _ca_today.brush = Brushes.DarkRed;
            _ca_today.font = new Font("Arial",12,FontStyle.Bold,GraphicsUnit.Pixel);
            _ca_today.rectangle = new Rectangle(213,94,99,14);

            _ca_overpay= new ContentArea();
            _ca_overpay.align = StringAlignment.Near;
            _ca_overpay.brush = Brushes.Red;
            _ca_overpay.font = new Font("Arial",12,FontStyle.Regular,GraphicsUnit.Pixel);
            _ca_overpay.rectangle = new Rectangle(144,94,70,14);

            _ca_inet= new ContentArea();
            _ca_inet.align = StringAlignment.Center;
            _ca_inet.brush = Brushes.Black;
            _ca_inet.font = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
            _ca_inet.rectangle = new Rectangle(132,108,90,33);

            _ca_tv= new ContentArea();
            _ca_tv.align = StringAlignment.Center;
            _ca_tv.brush = Brushes.Black;
            _ca_tv.font = new Font("Arial",11,FontStyle.Regular,GraphicsUnit.Pixel);
            _ca_tv.rectangle = new Rectangle(222,108,90,33);

            _ca_error= new ContentArea();
            _ca_error.align = StringAlignment.Center;
            _ca_error.brush = Brushes.Red;
            _ca_error.font = new Font("Arial",24,FontStyle.Bold,GraphicsUnit.Pixel);
            _ca_error.rectangle = new Rectangle(144,74,168,57);
            _ca_error.text = "Ошибка";


            _areas.AddRange(new ContentArea[]{_ca_total,_ca_today,_ca_inet,_ca_tv,_ca_overpay,_ca_error});
            foreach(ContentArea a in _areas) a.Update();

        }



        public bool IsHidden
        {
            get
            {
                return (TaskbarState == TaskbarNotifier.TaskbarStates.hidden);
            }
        
        }


        protected override void DrawText(Graphics grfx)
        {
	        if (titleText != null && titleText.Length != 0)
			{
				StringFormat sf = new StringFormat();
				sf.Alignment = StringAlignment.Center;
				sf.LineAlignment = StringAlignment.Center;
				sf.FormatFlags = StringFormatFlags.NoWrap;
				sf.Trimming = StringTrimming.EllipsisCharacter;				
    			grfx.DrawString(titleText, hoverTitleFont, Brushes.Black, TitleRectangle, sf);
			}

            foreach(ContentArea a in _areas) a.Paint(grfx);
       
        }


        public void UpdateInfo(VoiaInfoParser info,int persentDay,int persentTraff,int persentTraffToday)
        {
        
            titleText =  info.Time.ToLongTimeString();
            contentText = info.TraffTotal;

           
            if (info.IsError)
            {
                _ca_error.enabled = true;
                _ca_total.enabled = false;
                _ca_today.enabled = false;
                _ca_tv.enabled = false;
                _ca_inet.enabled = false;
                _ca_overpay.enabled = false;  

            } else
            {
                _ca_error.enabled = false;
                _ca_total.text = info.TraffTotal;
                _ca_total.enabled = true;


                if (info.TraffTodayInt!=0)
                {
                    _ca_today.text = info.TraffToday;
                    _ca_today.enabled = true;
                } else
                {
                    _ca_today.enabled = false;            
                }

                if (info.TVName.Length!=0)
                {
                    _ca_tv.text = info.TVName+"\r\n"+info.TVMoney;
                    _ca_tv.enabled = true;
                } else
                {
                    _ca_tv.enabled = false;            
                }

                _ca_inet.text = info.INETName+"\r\n"+info.INETMoney;
                _ca_inet.enabled = true;



                if (info.TraffOver!=0)
                {
                    _ca_overpay.text = info.TraffOverMoney;
                    _ca_overpay.enabled = true;
                } else
                {
                    _ca_overpay.enabled = false;            
                }

            }
            _chart.SetPersent(persentDay,persentTraff,persentTraffToday);


            if (!IsHidden) Refresh();
        }


        public void Tuch()
        {
            if (IsHidden)  Show(titleText,contentText,300,5000,1000);
            if (taskbarState == TaskbarStates.disappearing)
            {
                taskbarState = TaskbarStates.appearing;
            }
        }


        public void UpdateTimer(int persent)
        {
            if (taskbarState != TaskbarStates.hidden) _chart.SetPersentTimer(persent);
            
        }


    }
}
